<template>
  <div class="login">
    <el-card class="login_card">
      <el-image class="logo_image" :src="logo_url" fit="cover"></el-image>
      <p class="login_title">登 录</p>
      <p class="login_desc">欢迎登录学生成绩管理系统</p>
      <el-form
        ref="loginForm"
        :model="loginForm"
        :rules="loginRules"
        auto-complete="on"
      >
        <el-form-item prop="username">
          <el-input
            ref="username"
            v-model="loginForm.username"
            placeholder="Username"
            name="username"
            type="text"
            tabindex="1"
            auto-complete="on"
            prefix-icon="el-icon-user"
          />
        </el-form-item>

        <el-form-item prop="password">
          <el-input
            :key="passwordType"
            ref="password"
            v-model="loginForm.password"
            :type="passwordType"
            placeholder="Password"
            name="password"
            tabindex="2"
            auto-complete="on"
            prefix-icon="el-icon-lock"
            show-password
            @keyup.enter.native="handleLogin"
          />
        </el-form-item>
        <el-form-item prop="code">
          <el-input
            ref="code"
            type="text"
            class="input"
            tabindex="3"
            v-model="code"
            style="width: 200px"
            placeholder="IdentifyCode"
            prefix-icon="el-icon-key"
            @keyup.enter.native="handleLogin"
          />
          <div class="code" @click="refreshIdentifyCode">
            <SIdentify :identifyCode="identifyCode" />
          </div>
        </el-form-item>
      </el-form>
      <el-button
        :loading="loading"
        style="background: #5b89fe; color: white;margin-top:20px"
        @click="handleLogin"
        >Login</el-button
      >
    </el-card>
    <!-- footer -->
    <p class="footer">Copyright © XXX All Right Reserved</p>
  </div>
</template>

<script>
import { validUsername } from "@/utils/validate";
import SIdentify from "@/components/SIdentify";

export default {
  components: { SIdentify },
  name: "Login",
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error("The username cannot be empty"));
      } else {
        callback();
      }
    };
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error("The password must be at least 6 characters"));
      } else {
        callback();
      }
    };
    const validateCode = (rule, value, callback) => {
      if (this.code === '') {
        callback(new Error("The verification code cannot be empty"));
      } else if(this.code !== this.identifyCode) {
        callback(new Error("Verification code error"));
      }else {
        callback();
      }
    };
    return {
      logo_url: require("@/assets/img/logo.png"),
      loginForm: {
        username: "",
        password: "",
      },
      
      loginRules: {
        username: [
          { required: true, trigger: "blur", validator: validateUsername },
        ],
        password: [
          { required: true, trigger: "blur", validator: validatePassword },
        ],
        code: [
          { required: true, trigger: "blur", validator: validateCode },
        ],
      },
      loading: false,
      passwordType: "password",
      redirect: undefined,
      //密码登录图形验证码
      identifyCode: "",
      //生成图形验证码的依据
      identifyCodes: "1234567890abcdefghizklmnopqrstuvwxyz",
      //用户输入验证码内容
      code:''
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true,
    },
  },
  mounted() {
    // 初始化验证码
    this.identifyCode = "";
    this.makeIdentifyCode(4);
  },
  methods: {
    // 刷新验证码
    refreshIdentifyCode() {
      this.identifyCode = "";
      this.makeIdentifyCode(4);
    },
    // 生成4位数的随机验证码
    makeIdentifyCode(l) {
      for (let i = 0; i < l; i++) {
        this.identifyCode +=
          this.identifyCodes[this.randomNum(0, this.identifyCodes.length)];
      }
    },
    // 生成单个验证码
    randomNum(min, max) {
      return Math.floor(Math.random() * (max - min) + min);
    },

    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$store
            .dispatch("user/login", this.loginForm)
            .then(() => {
              this.$router.push({ path: "/" });
              this.loading = false;
            })
            .catch(() => {
              this.loading = false;
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>


<style scoped>
.login {
  width: 100%;
  height: 100%;
  background-image: url("~@/assets/img/bg.png");
  position: absolute;
  left: 0;
  top: 0;
  background-size: 100% 100%;
}
.login_card {
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  margin: auto;
  width: 386px;
  min-width: 386px;
  height: 502px;
  min-height: 502px;
  border-radius: 10px;
  text-align: center;
}
.logo_image {
  width: 60px;
  margin-top: 40px;
}
.login_title {
  margin: 25px 0;
  font-size: 25px;
  font-weight: bold;
}
.login_desc {
  letter-spacing: 2px;
  color: #999a9a;
  margin-bottom: 16px;
  font-size: 16px;
}
.el-button {
  width: 100%;
}

.footer {
  /* position: absolute;
  left: 0;
  right: 0;
  bottom: 20px; */
  margin-top: 900px;
  text-align: center;
  color: rgb(183, 181, 181);
}

.footer:hover {
  color: white;
}
.show-pwd {
  position: absolute;
  right: 10px;
  cursor: pointer;
}
.input {
  float: left;
}
.code {
  float: right;
}
</style>
